/*
 * FreeModbus Libary: LPC214X Port
 * Copyright (C) 2007 Tiago Prado Lone <tiago@maxwellbohr.com.br>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id: porttimer.c,v 1.1 2007/04/24 23:15:18 wolti Exp $
 */

/* ----------------------- Platform includes --------------------------------*/
#include "includes.h"
#include "port.h"

/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"

/* ----------------------- static functions ---------------------------------*/
static USHORT usTimer;

/* ----------------------- Start implementation -----------------------------*/
BOOL
xMBPortTimersInit( UCHAR mode,ULONG ulBaudRate)
{
    if(mode == MB_ASCII)
        {
        usTimer = 0xa473;//for ascii mode timeout 1s
        }else if(mode == MB_RTU){
        switch(ulBaudRate){
            case 1200:// 3.5 char  
                break;
            case 2400:// 3.5 char  
                break;
            case 4800:// 3.5 char  
                break;
            case 9600:// 3.5 char  
                break;
            case 14400:// 3.5 char  
                break;
            case 19200:// 3.5 char 2.005ms 
                 usTimer = 0xf480;
                break;
            case 32000:// 3.5 char  
                break;
            case 38400:// 3.5 char  
                break;
            case 115200:// 3.5 char 
                break;
            }
        }

    TMR0H = (usTimer>>8);             
    TMR0L = usTimer&0x00ff;	
        
    //Stops Timer0
    T0CONbits.TMR0ON = 0;
    //Timer0 is configured as a 16-bit timer/counter
    T0CONbits.T08BIT = 0;
    //Timer0 clock sourceselect internal instruction cycle clock
    T0CONbits.T0CS = 0;
    //Timer0 source edge select increment on low-to high transition on T0CKI 
    // pin,just aviable when T0CS=1
    T0CONbits.T0SE = 0;
    //Timer0 prescaler assignment bit,
    // 1 = Timer0 prescaler in not assigned, Timer0 clock input bypasses 
    // prescaler.
    // 0 = Timer0 prescaler is assigned.Timer0 clock input comes from prescaler 
    // output.
    T0CONbits.PSA = 0;
    if(mode == MB_ASCII)
        {
        //T0PS2:T0PS0:  Timer0 Prescaler Select bits    
        //111  = 1:256 prescale value
        //110  = 1:128 prescale value
        //101  = 1:64   prescale value
        //100  = 1:32   prescale value
        //011  = 1:16   prescale value
        //010  = 1:8     prescale value
        //001  = 1:4     prescale value
        //000  = 1:2     prescale value
        T0CONbits.T0PS2 = 1;
        T0CONbits.T0PS1 = 1;
        T0CONbits.T0PS0 = 1;
        }else if(mode == MB_RTU)
        {
        T0CONbits.T0PS2 = 0;
        T0CONbits.T0PS1 = 0;
        T0CONbits.T0PS0 = 1;
        }
    return TRUE;
}


void
vMBPortTimersEnable(  )
{
    TMR0H = (usTimer>>8);             
    TMR0L = usTimer&0x00ff;	
    
    T0CONbits.TMR0ON = 1; // Enable Timer
}

void
vMBPortTimersDisable(  )
{
    T0CONbits.TMR0ON = 0;//stops Timer0
}

void            vMBPortTimersDelay( USHORT usTimeOutMS )
{
}

void
prvvTIMERExpiredISR( void )
{
    ( void )pxMBPortCBTimerExpired(  );

   TMR0H = (usTimer>>8);             
   TMR0L = usTimer&0x00ff;	
}
